package com.lyouu.dataminer.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.Days;

public class Util {
	private static Logger logger = Logger.getLogger(Util.class);

	public static String replaceHtmlCodeToSpecialCharacter(String str) {
		str = str.replace("&auml;", "ä");
		str = str.replace("&ouml;", "ö");
		str = str.replace("&uuml;", "ü");
		str = str.replace("&szlig;", "ß");
		return str;
	}

	public static String getCurrency(String priceStr) {
		if (priceStr.contains(Constants.CURRENCY_CNY_SIGN))
			return Constants.CURRENCY_CNY;
		if (priceStr.contains(Constants.CURRENCY_USD_SIGN))
			return Constants.CURRENCY_USD;
		if (priceStr.contains(Constants.CURRENCY_GPB_SIGN))
			return Constants.CURRENCY_GPB;
		else
			return Constants.CURRENCY_EUR;
	}

	public static String getCurrencySign(String priceStr) {
		if (priceStr.contains(Constants.CURRENCY_CNY_SIGN))
			return Constants.CURRENCY_CNY_SIGN;
		else if (priceStr.contains(Constants.CURRENCY_USD_SIGN))
			return Constants.CURRENCY_USD_SIGN;
		else if (priceStr.contains(Constants.CURRENCY_GPB_SIGN))
			return Constants.CURRENCY_GPB_SIGN;
		else
			return Constants.CURRENCY_EUR_SIGN;
	}

	public static String getMinPrice(ArrayList<String> priceList) {
		String min = "1000000";
		if (!priceList.isEmpty()
				&& !priceList.get(0).equals(Constants.UNKNOWN_PRICE)) {
			for (String price : priceList) {
				if (Float.valueOf(min) > Float.valueOf(price))
					min = price;
			}
		}
		if (min.equals("1000000")) {
			min = Constants.UNKNOWN_PRICE;
		}
		return min;
	}

	public static String formatPrice(double d) {
		DecimalFormat df = new java.text.DecimalFormat("#.00");
		return String.valueOf(df.format(d));
	}

	public static int calcDuration(String startDate, String endDate) {
		int days = -1;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		DateTime start;
		try {
			start = new DateTime(sdf.parse(startDate));
			DateTime end = new DateTime(sdf.parse(endDate));
			days = Days.daysBetween(start, end).getDays();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return days + 1;
		}
	}
}
